# -*- coding: utf-8 -*-
import os
import zipfile
from  pysql import *


class DealDownLoad:
    #试卷所在目录
    def __init__(self, workdir):
        self.workdir = workdir;
        return;
    #解压文件
    #path 待解压的文件路径
    #extrapath 解压的目的路径
    def unzip(self, path, extrapath):
        f = zipfile.ZipFile(path, 'r')
        for file in f.namelist():
            f.extract(file, extrapath);
    #遍历每个scrappaer目录下是否有zip文件，如果有zip文件，先解压文件
    def iterPaper(self, fname, paperpth):
        if(not os.path.isdir(paperpth)):
            return;
        files = os.listdir(paperpth)
        # 如果有zip文件，先解压zip文件
        for f in files:
            fpath = os.path.join(paperpth,f)
            if (f.endswith('zip')):
                self.unzip(fpath, paperpth)
        # 重新遍历文件目录，只保留doc文件，其余的删除掉
        files = os.listdir(paperpth)
        for f in files:
            fpath = os.path.join(paperpth,f)
            if (not f.endswith('doc')):
                os.remove(fpath)
    def preZipFiles(self):
        files = os.listdir(self.workdir)
        for f in files:
            # f的名称即为试卷id
            fpath = os.path.join(self.workdir, f)
            #依次遍历每个试卷文件夹
            self.iterPaper(f,fpath)
workdir='/work/temp/xuekewang'
dd=DealDownLoad(workdir);
dd.preZipFiles()